package orest.common.dao;

//import java.util.*;
import java.sql.*;
//import javax.sql.*;
//import expertappraise.common.response.Response;
import orest.common.util.DataBase;
import orest.common.valueobject.UserSessionInfo;
//import expertappraise.common.util.Tools;

/**
 * This class provide DB interface for getting user info 
 * and expert info from the table T_UserInfo and T_Expert.
 * 
 * @author shilei
 *
 */
public class UserSessionDao {
	
	/** Creates a new instance of UserSessionDao */
    public UserSessionDao () {
    }
    
    public boolean getSessionInfo(String userid,
    		String password, UserSessionInfo sessionInfo) {
    	
    	boolean result = true;  
        ResultSet rs = null; 
     
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();
        String temp = "";
        
        String sql = "select * from T_FrontUser where (UserID = '";
        sql += userid + "' and Password = '" + password + "') or (UserID = '"+ userid +"' and Password = '" + password + "')";
       
        
        try{
            conn = db.getSysConnectionPool ();
            pstmt = conn.prepareStatement (sql);
            rs = pstmt.executeQuery ();
            
            if (rs.next()){                
                           
                sessionInfo.setUserID(rs.getString("UserID"));
                if (rs.getString("UserName") != null) {
                	sessionInfo.setUserName(rs.getString("UserName"));
                }else {
                	sessionInfo.setUserName("");
                }
                
                sessionInfo.setPassword(rs.getString("Password"));
                if (rs.getString("UnitName") != null) {
                	sessionInfo.setUnitName(rs.getString("UnitName"));
                }else {
                	sessionInfo.setUnitName("");
                }
                if (rs.getString("UserType") != null) {
                	sessionInfo.setUserType(rs.getString("UserType"));
                }else {
                	sessionInfo.setUserType("");
                }
                if (rs.getString("EnjoyType") != null) {
                	sessionInfo.setEnjoyType(rs.getString("EnjoyType"));
                }else {
                	sessionInfo.setEnjoyType("");
                }
                 if (rs.getString("IsEnabled") != null) {
                	sessionInfo.setIsEnabled(rs.getString("IsEnabled"));
                }else {
                	sessionInfo.setIsEnabled("");
                }
                 if (rs.getString("IsCheck") != null) {
                	sessionInfo.setIsCheck(rs.getString("IsCheck"));
                }else {
                	sessionInfo.setIsCheck("");
                }
                if (Double.valueOf(rs.getDouble("RemainMoney")) != null) {
                	sessionInfo.setRemainMoney(rs.getDouble("RemainMoney"));
                }else {
                	sessionInfo.setRemainMoney(rs.getDouble("0"));
                }
                
            }
             return result;

    }
        catch(SQLException e){
            e.printStackTrace();
            result = false;
            return result;
        }
        catch(Exception e){
        	e.printStackTrace();
            result = false;
            return result;
        }


        finally{
            /* close the statement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

            /* close the rs */
            if(null != rs){
                try{
                    rs.close ();
                    rs = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

            /* release the conn */
            if(null != conn){
                try{
                    conn.close ();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        }
    }

    public boolean getSessionInfo(String userid, UserSessionInfo sessionInfo) {

    	boolean result = true;
        ResultSet rs = null; 
       
        PreparedStatement pstmt = null;
        Connection conn = null;
        DataBase db = new DataBase();
        String temp = "";

        String sql = "select * from T_FrontUser where UserID = '";
        sql += userid + "'";
       
        try{
            conn = db.getSysConnectionPool ();
            pstmt = conn.prepareStatement (sql);
            rs = pstmt.executeQuery ();

            if (rs.next()){
                 sessionInfo.setUserID(rs.getString("UserID"));
                if (rs.getString("UserName") != null) {
                	sessionInfo.setUserName(rs.getString("UserName"));
                }else {
                	sessionInfo.setUserName("");
                }

                sessionInfo.setPassword(rs.getString("Password"));
                if (rs.getString("UnitName") != null) {
                	sessionInfo.setUnitName(rs.getString("UnitName"));
                }else {
                	sessionInfo.setUnitName("");
                }
                if (rs.getString("UserType") != null) {
                	sessionInfo.setUserType(rs.getString("UserType"));
                }else {
                	sessionInfo.setUserType("");
                }
                if (rs.getString("EnjoyType") != null) {
                	sessionInfo.setEnjoyType(rs.getString("EnjoyType"));
                }else {
                	sessionInfo.setEnjoyType("");
                }
                 if (rs.getString("IsEnabled") != null) {
                	sessionInfo.setIsEnabled(rs.getString("IsEnabled"));
                }else {
                	sessionInfo.setIsEnabled("");
                }
                 if (rs.getString("IsCheck") != null) {
                	sessionInfo.setIsCheck(rs.getString("IsCheck"));
                }else {
                	sessionInfo.setIsCheck("");
                }
                if (Double.valueOf(rs.getDouble("RemainMoney")) != null) {
                	sessionInfo.setRemainMoney(rs.getDouble("RemainMoney"));
                }else {
                	sessionInfo.setRemainMoney(rs.getDouble("0"));
                }

               

            }
            return result;
        }

        catch(SQLException e){
            e.printStackTrace();
            result = false;
            return result;
        }
        catch(Exception e){
        	e.printStackTrace();
            result = false;
            return result;
        }
        finally{
            /* close the statement */
            if(null != pstmt){
                try{
                    pstmt.close ();
                    pstmt = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

            /* close the rs */
            if(null != rs){
                try{
                    rs.close ();
                    rs = null;
                }
                catch(SQLException e){
                	e.printStackTrace();
                }
            }

          

            /* release the conn */
            if(null != conn){
                try{
                    conn.close ();
                    conn = null;
                    db = null;
                }
                catch(Exception e){
                	e.printStackTrace();
                }
            }
        }
    }

}
